# -*- coding: utf-8 -*-
# @Author  : Lison Song
# @Time    : 2023/3/16 18:13
import uuid
import urllib.parse
from datetime import datetime, timedelta


class MyTools:
    @staticmethod
    def getUUID(content):
        '''
        uuid 生成器
        :param content:
        :return: uuid
        '''
        return str(uuid.uuid3(uuid.NAMESPACE_DNS, content)).replace("-", "")

    # 公众号文章URL预处理
    @staticmethod
    def pre_wx_article_url(urlstr):
        '''
        公众号文章URl 处理
        :param urlstr:  公众号文章原始链接
        :return:
        '''
        result_url = ""
        parameterList = ['__biz', 'mid', 'idx', 'sn']
        r = urllib.parse.urlparse(urlstr)
        querydic = dict(urllib.parse.parse_qsl(urllib.parse.urlsplit(urlstr).query))
        if not r.query:
            return urlstr
        else:
            result_url_head = f"{r.scheme}://{r.netloc}{r.path}?"
            new_queryList = []
            for key, value in querydic.items():
                if key in parameterList:
                    new_queryList.append(f"{key}={value}")
            result_url = result_url_head + "&".join(new_queryList)
            if len(new_queryList) < len(parameterList):
                return urlstr
            return result_url

    @staticmethod
    def getDatetimestr(obj):
        '''
        日期时间对象格式化
        :param obj: 日期时间对象
        :return:
        '''
        return datetime.strftime(obj, "%Y-%m-%d %H:%M:%S")

    @staticmethod
    def dateformartforvar(datestr):
        '''
        输入时间字符串 返回 日期 时间对象 及日期字符串 时间字符串
        :param datestr: %Y-%m-%d %H:%M:%S 格式的时间字符串
        :return:
        '''
        object_date = datetime.strptime(datestr, "%Y-%m-%d %H:%M:%S").date()
        object_time = datetime.strptime(datestr, "%Y-%m-%d %H:%M:%S").time()
        day = object_date.strftime("%Y-%m-%d")
        time = object_time.strftime("%H:%M:%S")
        return object_date, object_time, day, time

    @staticmethod
    def getTodaydatestr():
        '''
         获取当天日期字符串
        :return:
        '''
        now = datetime.now()
        return now.strftime("%Y-%m-%d")

    @staticmethod
    def getdatestrList(days: int = 1):
        '''
        返回指定天数之前的日期字符串列表
        :param days:
        :return:
        '''
        result = []
        for i in range(days, 0, -1):
            obj = datetime.now() - timedelta(days=i)
            # print(obj)
            result.append(obj.strftime("%Y-%m-%d"))
        return result


if __name__ == '__main__':
    print(MyTools.getUUID('https://mp.weixin.qq.com/s/Sln-Mc4NKJDdjvrJGBiKm'))
    urlstr = "https://mp.weixin.qq.com/s?__biz=MzU2MzUwNjA2MA==&mid=2247586980&idx=1&sn=cba727ab89f28e37d2e3884fb22704b7&chksm=fc5aea47cb2d6351c11645a60dc03621e34af1b312796472905acc836c4b833cab9af16aa6c1&scene=126&sessionid=1679493621&subscene=7&key=67b1c1a02b0dd5bebd261cb252bb28f00c9cec7efa764b03de2638baab8368aa7eff0f2f8cd4716e6a264094a59e9edd4a44db5dea4ef4589d3789e615ff11d8786614658139ee04340fc452c733395de8af7f4beaf038a13008d4048f63908d85f22cb56cb1491d1cde7e128217e6d4709bebc6b67135f06192b98cb4d4f4ee&ascene=7&uin=NTY2NTkyOTI1&devicetype=Windows+10+x64&version=6309001c&lang=zh_CN&exportkey=n_ChQIAhIQRAcOPsxVEJ4wlwJz1J8X0hLgAQIE97dBBAEAAAAAAELsLfrKYBcAAAAOpnltbLcz9gKNyK89dVj04E6nK20g4t4rnDSyF6A1bwX1AkLtIwDZ4JPIKRGk2Xoq49OHwP%2FcXfjSKDcE8xH613n8lgTUMgp6zW4TA8IDLTZpLnd4qnECTiti8G1aHzDcl3TWNtkdeLc1uQBQWrmH7moJjzC5PMqDcxNnLd3Hh%2BiiJBZ%2BltBNlMvlHnAP86vCBZpCKTsI%2FjcdGqBxVZujJgbWXi8wNEFTKZNajCxVZ1xqVTZGo5YKdTh9wTvdZ9rIlyaYpVjvho3%2B&acctmode=0&pass_ticket=rRJ4zAr0WD0DWtWqH1LwHZ3kK8y9AvU0XMKTrWq6Mg9dkMHSoWoT76g6Ozyij6I9zBMunirkqLpcEaalh8I19A%3D%3D&wx_header=1&fontgear=2"
    # urlstr = "https://mp.weixin.qq.com/s/w6CK8cOW16C77iOUeKufMg"
    result_url = MyTools.pre_wx_article_url(urlstr=urlstr)
    print(result_url)
    MyTools.dateformartforvar('2023-03-24 14:34:00')
    print(MyTools.getTodaydatestr())
    print(MyTools.getdatestrList(7))
